package com.sumainfor.pc.gmio.service.impl;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.gmio.dao.DiForceMapper;
import com.sumainfor.pc.gmio.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.gmio.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.gmio.service.DiForceService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 退租账单
 */
@Service
public class DiForceSerivceImpl implements DiForceService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(DiForceSerivceImpl.class);
    @Autowired
    private DiForceMapper diForceMapper;
    @Autowired
    private ZpcMktFeign zpcMktFeign;
    @Autowired
    private ZzuulFeign zzuulFeign;

    /*
     * 查询强制退列表
     * */
    @Override
    public List<Map<String, Object>> getForceList(Map<String, Object> params) {
        List<Map<String, Object>> gmioList = diForceMapper.getForceList(params);
        //保存强制退租对象的编号
        List<String> TenantList = new ArrayList<String>();
        //保存强制退申请人的编号
        List<String> sysUserList = new ArrayList<String>();
        for (Map<String, Object> map : gmioList) {
            if (map.get("TenId") != null) {
                TenantList.add(map.get("TenId").toString());
            }
            if (map.get("AppId") != null) {
                sysUserList.add(map.get("AppId").toString());
            }
        }
        params.put("TenantList", TenantList);

        //到市场部查询退租对象信息
        JsonResult result = zpcMktFeign.getTenantListByTenId(params);
        System.out.println("市场部返回的值 :" + result);
        List<Map<String, Object>> marketList = (List<Map<String, Object>>) result.get("result");
        for (Map<String, Object> marketMap : marketList) {
            for (Map<String, Object> gmioMap : gmioList) {
                if (MapUtils.getString(marketMap, "TenId").equals(MapUtils.getString(gmioMap, "TenId"))) {
                    gmioMap.putAll(marketMap);
                }
            }
        }
        params.put("sysUserList", sysUserList);
        //网关查询强制退申请人信息
        JsonResult zuulResult = zzuulFeign.getReapirSysUser(params);
        System.out.println("网关返回的值:" + zuulResult);
        List<Map<String, Object>> zuulList = (List<Map<String, Object>>) zuulResult.get("result");
        for (Map<String, Object> zuulMap : zuulList) {
            for (Map<String, Object> gmioMap : gmioList) {
                if (MapUtils.getString(zuulMap, "SysUserID").equals(MapUtils.getString(gmioMap, "AppId"))) {
                    gmioMap.putAll(zuulMap);
                }
            }
        }
        return gmioList;
    }

    /**
     * 查询强制退总数
     */
    @Override
    public Integer getForceListCount(Map<String, Object> params) {
        return diForceMapper.getForceListCount(params);
    }

    /**
     * 审核退租申请
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult updateDiForce(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            try {
                params.put("by", ContextUtils.getUserId());//操作人
                params.put("term", "4");// 更新端 Update端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonUpdateParms(params);
                params.put("StatusTime", params.get("updtm"));//强制退审核时间
                if (diForceMapper.updateDiForce(params) > 0) {
                    result.putSuccess("操作成功");
                } else {
                    result.putFailed("服务器繁忙");
                }
            } catch (Exception e) {
                result.putFailed("服务器繁忙");
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } finally {
            lock.unlock();
        }
        return result;
    }
    /**
     * 服务管家查询强制退列表
     */
    @Override
    public List<Map<String,Object>> getDiForceApplyList(Map<String,Object> params){
        List<Map<String,Object>> list=diForceMapper.getForceList(params);
        List<String> houInfoIdList=new ArrayList<String>();
        for (Map<String,Object> map : list){
            if (map.get("HouInfoId")!=null){
                houInfoIdList.add(map.get("HouInfoId").toString());
            }
        }
        //到市场部查询房源信息
        params.put("houInfoIdList",houInfoIdList);
        JsonResult result=zpcMktFeign.getHouPactApplyList(params);
        System.out.println("市场部返回的参数:"+result);
        if (result.get("errorCode").equals("0")) {
            //市场部的房源列表
            List<Map<String,Object>> mktlist=(List<Map<String,Object>>)result.get("result");
            for (Map<String,Object> giomap : list){
                if (giomap.get("HouInfoId")!=null){
                    for (Map<String,Object> mktmap : mktlist) {
                        if (mktmap.get("HouInfoId")!=null) {
                            if (MapUtils.getString(giomap, "HouInfoId").equals(MapUtils.getString(mktmap, "HouInfoId"))) {
                                giomap.putAll(mktmap);
                            }
                        }
                    }
                }

            }
        }
        return list;
    }
    /*
     * 查询退租申请总数
     * */
    public Integer getDiForceApplyListCount(Map<String, Object> params){
        return diForceMapper.getForceListCount(params);
    }
}
